package com.appspot.bibtable.servlet;

import java.io.IOException;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appspot.bibtable.PMF;
import com.appspot.bibtable.model.UserInfo;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class UnbookmarkItemServlet extends BookmarkItemServletBase
{
	private static final Logger logger = Logger.getLogger(UnbookmarkItemServlet.class.getName());

	@Override
	protected void doAction(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		UserService service = UserServiceFactory.getUserService();
		PersistenceManager manager = PMF.get().getPersistenceManager();
		try
		{
			UserInfo userInfo = UserInfo.getUserInfoOf(service.getCurrentUser(), manager);

			if (userInfo != null)
			{
				Key key = getKey(request);
				userInfo.unbookmark(key);
			}
			else
			{
				logger.info("Bookmarking request from non-registered user (" + request.getRemoteAddr() + ").");
				response.sendError(HttpServletResponse.SC_FORBIDDEN);
			}
		}
		finally
		{
			manager.close();
		}
	}
}
